package com.example.controller;

import com.example.common.Result;
import com.example.common.PageResult;
import com.example.dto.StoreProductDTO;
import com.example.dto.StoreProductUpdateDTO;
import com.example.service.StoreProductService;
import com.example.vo.StoreProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 店长商品控制器
 */
@RestController
@RequestMapping("/api/store-product")
@CrossOrigin
public class StoreProductController {
    
    @Autowired
    private StoreProductService storeProductService;
    
    /**
     * 店长上架商品
     */
    @PostMapping
    public Result<Void> addStoreProduct(@RequestBody StoreProductDTO storeProductDTO) {
        return storeProductService.addStoreProduct(storeProductDTO);
    }
    
    /**
     * 删除店长商品
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteStoreProduct(@PathVariable Long id) {
        return storeProductService.deleteStoreProduct(id);
    }
    
    /**
     * 更新店长商品
     */
    @PutMapping("/{id}")
    public Result<Void> updateStoreProduct(@PathVariable Long id, @RequestBody StoreProductDTO storeProductDTO) {
        return storeProductService.updateStoreProduct(id, storeProductDTO);
    }
    
    /**
     * 根据ID查询店长商品
     */
    @GetMapping("/{id}")
    public Result<StoreProductVO> getStoreProductById(@PathVariable Long id) {
        return storeProductService.getStoreProductById(id);
    }
    
    /**
     * 分页查询当前店长的商品
     */
    @GetMapping("/page")
    public Result<PageResult<StoreProductVO>> getStoreProductPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer status) {
        return storeProductService.getStoreProductPage(pageNum, pageSize, keyword, status);
    }
    
    /**
     * 查询店长所有上架商品（用于用户购买）
     */
    @GetMapping("/online")
    public Result<PageResult<StoreProductVO>> getOnlineProducts(
            @RequestParam(value = "storeManagerId", required = false) String storeManagerIdStr,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String keyword) {
        
        // 处理storeManagerId参数，防止"null"字符串导致的转换错误
        Long storeManagerId = null;
        if (storeManagerIdStr != null && !storeManagerIdStr.trim().isEmpty() && !"null".equals(storeManagerIdStr)) {
            try {
                storeManagerId = Long.parseLong(storeManagerIdStr);
            } catch (NumberFormatException e) {
                // 如果转换失败，使用null值，表示查询所有店长的商品
                storeManagerId = null;
            }
        }
        
        return storeProductService.getOnlineProducts(storeManagerId, pageNum, pageSize, keyword);
    }
    
    /**
     * 上架/下架商品
     */
    @PutMapping("/{id}/status")
    public Result<Void> updateProductStatus(@PathVariable Long id, @RequestParam Integer status) {
        return storeProductService.updateProductStatus(id, status);
    }
    
    /**
     * 商品入库
     */
    @PostMapping("/{id}/stock-in")
    public Result<Void> stockIn(@PathVariable Long id, @RequestParam Integer quantity) {
        return storeProductService.stockIn(id, quantity);
    }

    /**
     * 根据供货商品ID查询店长商品库存
     */
    @GetMapping("/stock-by-supply-product/{supplyProductId}")
    public Result<Integer> getStockBySupplyProductId(@PathVariable Long supplyProductId) {
        return storeProductService.getStockBySupplyProductId(supplyProductId);
    }

    /**
     * 更新店长商品字段信息（如最低库存等）
     */
    @PutMapping("/{id}/fields")
    public Result<Void> updateStoreProductFields(@PathVariable Long id, @RequestBody StoreProductUpdateDTO updateDTO) {
        return storeProductService.updateStoreProduct(id, updateDTO);
    }
    
    /**
     * 获取店长已进货但未上架的商品列表
     */
    @GetMapping("/available-for-shelf")
    public Result<PageResult<StoreProductVO>> getAvailableProductsForShelf(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String keyword) {
        return storeProductService.getAvailableProductsForShelf(pageNum, pageSize, keyword);
    }
    
    /**
     * 获取公开商品详情（用于用户查看和购买）
     */
    @GetMapping("/public/{id}")
    public Result<StoreProductVO> getPublicProductDetail(@PathVariable Long id) {
        return storeProductService.getPublicProductDetail(id);
    }
}